package com.fengke.csc.action.order;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.apache.log4j.Logger;

import com.fengke.csc.action.BaseAction;
import com.fengke.service.OrderService;
import com.fengke.service.vo.OrderVO;
import com.fengke.service.vo.PaymentVO;
import com.fengke.util.Constants;
import com.fengke.util.LoggerFactory;
import com.fengke.util.StringUtil;

public class OrderAction extends BaseAction {
	private static final long serialVersionUID = -3184787725929491951L;
	private Logger logger = LoggerFactory.getLogger(OrderAction.class);
	private OrderService orderService;
	private List<OrderVO> orderVOList = new ArrayList<OrderVO>();
	private OrderVO orderVO = new OrderVO();
	private List<PaymentVO> paymentVOList = new ArrayList<PaymentVO>();
	private PaymentVO paymentVO = new PaymentVO();
	public String showOrderList() {
		logger.debug("showOrderList...");
		if(orderVO == null) {
			orderVO = new OrderVO();
		}
		if(getPageBean().getCurrentPage() == null || getPageBean().getCurrentPage() <= 0) {
			getPageBean().setCurrentPage(1);
		}
		
		if(getPageBean().getPageSize() == null || getPageBean().getPageSize() <= 0) {
			getPageBean().setPageSize(Constants.PageSize.CSC_PAGE_SIZE);
		}
		// 获取总量
		Integer orderVOListSize = null;
		try {
			orderVOListSize = orderService.researchOrderListSize(orderVO);
		} catch (Exception e) {
			logger.error("检索订单总量异常！orderVO=" + orderVO);
		}
		if(orderVOListSize != null && orderVOListSize > 0) {
			getPageBean().setResultItemCount(orderVOListSize);
			try {
				orderVOList = orderService.researchOrderList(orderVO, (getPageBean().getCurrentPage() - 1) * getPageBean().getPageSize(), getPageBean().getPageSize());
			} catch (Exception e) {
				logger.error("检索订单失败！orderVO=" + orderVO, e);
				this.addPageDangerMessage("检索订单失败！");
			}
		} else {
			getPageBean().setResultItemCount(0);
		}
		
		return Constants.ActionReturnName.SUCCESS;
	}
	/**
	 * 展示订单详情
	 * @return
	 */
	public String showOrderDetail() {
		if(orderVO == null || orderVO.getOrderModel() == null || orderVO.getOrderModel().getId() == null) {
			this.addPageDangerMessage("订单号为空！");
			return INPUT;
		}
		Long orderId = orderVO.getOrderModel().getId();
		try {
			orderVO = orderService.researchOrderById(orderVO);
		} catch (Exception e) {
			logger.error("检索订单失败！orderId=" + orderId, e);
			this.addPageDangerMessage("检索订单失败！");
			return INPUT;
		}
		if(orderVO == null) {
			this.addPageDangerMessage("检索订单失败！");
			return INPUT;
		}
		return Constants.ActionReturnName.SUCCESS;
	}
	/**
	 * 删除订单
	 */
	public String deleteOrder() {
		String deletedOrderIds = ((String[]) (this.getValue().get("deletedOrderIds")))[0];
		if(deletedOrderIds == null || StringUtil.isEmpty(deletedOrderIds)) {
			this.addPageWarningMessage("待删除条目列表为空！");
			return Constants.ActionReturnName.SUCCESS;
		}
		String [] ids = deletedOrderIds.split(" ");
		try {
			orderService.deleteOrderByIds(Arrays.asList(ids));
		} catch (Exception e) {
			logger.error("删除订单失败！ids=" + ids, e);
			this.addPageDangerMessage("删除订单失败！");
			return Constants.ActionReturnName.SUCCESS;
		}
		this.addPageSuccessMessage("删除成功");
		return Constants.ActionReturnName.SUCCESS;
	}
	
	public String paymentManager() {
		logger.debug("paymentManager...");
		if(paymentVO == null) {
			paymentVO = new PaymentVO();
		}
		if(getPageBean().getCurrentPage() == null || getPageBean().getCurrentPage() <= 0) {
			getPageBean().setCurrentPage(1);
		}
		
		if(getPageBean().getPageSize() == null || getPageBean().getPageSize() <= 0) {
			getPageBean().setPageSize(Constants.PageSize.CSC_PAGE_SIZE);
		}
		// 获取总量
		Integer paymentVOListSize = null;
		try {
			paymentVOListSize = orderService.researchPaymentVOListSize(paymentVO);
		} catch (Exception e) {
			logger.error("检索支付单总量异常！paymentVO=" + paymentVO);
		}
		if(paymentVOListSize != null && paymentVOListSize > 0) {
			getPageBean().setResultItemCount(paymentVOListSize);
			try {
				paymentVOList = orderService.researchPaymentVOList(paymentVO, (getPageBean().getCurrentPage() - 1) * getPageBean().getPageSize(), getPageBean().getPageSize());
			} catch (Exception e) {
				logger.error("检索支付单失败！paymentVO=" + paymentVO, e);
				this.addPageDangerMessage("检索支付单失败！");
			}
		} else {
			getPageBean().setResultItemCount(0);
		}
		
		return Constants.ActionReturnName.SUCCESS;
	}
	public String showPaymentDetail() {
		if(paymentVO == null || paymentVO.getPaymentModel() == null || paymentVO.getPaymentModel().getId() == null) {
			this.addPageDangerMessage("付款单号为空！");
			return INPUT;
		}
		Long paymentId = paymentVO.getPaymentModel().getId();
		try {
			paymentVO = orderService.researchPaymentVOById(paymentVO);
		} catch (Exception e) {
			logger.error("检索付款单失败！paymentId=" + paymentId, e);
			this.addPageDangerMessage("检索付款单失败！");
			return INPUT;
		}
		if(paymentVO == null) {
			this.addPageDangerMessage("检索付款单失败！");
			return INPUT;
		}
		return Constants.ActionReturnName.SUCCESS;
	}
	public OrderService getOrderService() {
		return orderService;
	}
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	public List<OrderVO> getOrderVOList() {
		return orderVOList;
	}
	public void setOrderVOList(List<OrderVO> orderVOList) {
		this.orderVOList = orderVOList;
	}
	public OrderVO getOrderVO() {
		return orderVO;
	}
	public void setOrderVO(OrderVO orderVO) {
		this.orderVO = orderVO;
	}
	public List<PaymentVO> getPaymentVOList() {
		return paymentVOList;
	}
	public void setPaymentVOList(List<PaymentVO> paymentVOList) {
		this.paymentVOList = paymentVOList;
	}
	public PaymentVO getPaymentVO() {
		return paymentVO;
	}
	public void setPaymentVO(PaymentVO paymentVO) {
		this.paymentVO = paymentVO;
	}
}
